#include "src/gui/qt/subwindow.h"
#include "src/core/generic/translator.h"
#include <QMessageBox>
#include <QPushButton>

SubWindow::SubWindow(QMdiArea::ViewMode mode, QWidget *parent) : QMdiSubWindow(parent)
{
  extern Translator translator;
  setAttribute(Qt::WA_DeleteOnClose);
  setWindowFlags(windowFlags() ^ Qt::WindowMaximizeButtonHint);
  onTop = false;

  closeAction = new QAction(translator.qString("Close"), this);
  closeAction->setStatusTip(translator.qString("Close current tab", "action's status tip"));
  closeAction->setIcon(QIcon(":/actions/document-close.png"));
  connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
  onTopAction = new QAction(translator.qString("On Top"), this);
  onTopAction->setCheckable(true);
  onTopAction->setChecked(onTop);
  onTopAction->setEnabled(mode == QMdiArea::SubWindowView);
  onTopAction->setStatusTip(translator.qString("Make window staying always on top"));
  connect(onTopAction, SIGNAL(triggered()), this, SLOT(setOnTop()));

  // small hack to create custom cntext menu
  // standart way ( setContextMenuPolicy() ) seems to be buggy in this widget
  QMenu* contextMenu = qFindChild<QMenu*>(this);
  QList<QAction*> list = contextMenu->actions();
  for (QList<QAction*>::iterator i = list.begin(), e = list.end(); i != e; ++i) contextMenu->removeAction(*i);
  contextMenu->addAction(closeAction);
  separatorAction = contextMenu->addSeparator();
  contextMenu->addAction(onTopAction);
}

void SubWindow::closeEvent ( QCloseEvent * closeEvent )
{
  if (isWindowModified())
  {
    QMessageBox msgBox;
    msgBox.setIcon(QMessageBox::Warning);
    msgBox.setText(tr("The document \"%1\" has been modified.", "msg box text").arg(windowTitle()));
    msgBox.setInformativeText(tr("Do you wish to save it?", "msg box informative text"));
    QPushButton *yesButton = new QPushButton(tr("Yes", "button text"));
    yesButton->setIcon(QIcon(":/actions/dialog-ok-apply.png"));
    QPushButton *noButton = new QPushButton(tr("No", "button text"));
    noButton->setIcon(QIcon(":/actions/dialog-close.png"));
    QPushButton *cancelButton = new QPushButton(tr("Cancel", "button text"));
    cancelButton->setIcon(QIcon(":/actions/dialog-cancel.png"));
    msgBox.addButton(yesButton, QMessageBox::YesRole);
    msgBox.addButton(noButton, QMessageBox::NoRole);
    msgBox.addButton(cancelButton, QMessageBox::RejectRole);
    msgBox.setDefaultButton(yesButton); // cancelButton was defined as Esc button automatically
    msgBox.setWindowTitle(tr("VizMF : Warning", "msg box title"));
    msgBox.exec();
    delete yesButton;
    delete noButton;
    delete cancelButton;
    if (msgBox.clickedButton() == yesButton)
    {
      emit savingRequested(workspaceId);
      emit closed(workspaceId);
      closeEvent->accept();
    } else if (msgBox.clickedButton() == noButton)
    {
      emit closed(workspaceId);
      closeEvent->accept();
    } else closeEvent->ignore();
  } else
  {
    emit closed(workspaceId);
    closeEvent->accept();
  }
}

void SubWindow::hideEvent (QHideEvent *hideEvent)
{
  hideEvent->accept();
  emit visibilityChanged(false);
}

void SubWindow::showEvent (QShowEvent *showEvent)
{
  showEvent->accept();
  emit visibilityChanged(true);
}
